public class Text4 {
    ListNode re(ListNode head){
        //翻转链表
        if(head == null){
            return null;
        }

        ListNode cur = head;
        ListNode node = null;

        while(cur != null){
            ListNode tail = cur.next;
            cur.next = node;
            node = cur;
            cur = tail;
        }
        return node;
    }
    public ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        if(head1 == null)
            return head2;
        if(head2 == null)
            return head1;

        ListNode head = new ListNode(-1);
        ListNode pHead = head;

        int tmp = 0;
        while(head1 != null || head2 != null){
            int val = tmp;
            if(head1 != null){
                val += head1.val;
                head1 = head1.next;
            }

            if(head2 != null){
                val += head2.val;
                head2 = head2.next;
            }

            tmp = val/10;
            pHead.next = new ListNode(val%10);
            pHead = pHead.next;
        }

        if(tmp > 0){
            pHead.next = new ListNode(1);
        }

        return re(head.next);

    }
}
